package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateBanner
//@description: 创建Banner记录
//@param: banner model.Banner
//@return: err error

func CreateBanner(banner model.Banner) (err error) {
	err = global.GVA_DB.Create(&banner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBanner
//@description: 删除Banner记录
//@param: banner model.Banner
//@return: err error

func DeleteBanner(banner model.Banner) (err error) {
	err = global.GVA_DB.Delete(&banner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteBannerByIds
//@description: 批量删除Banner记录
//@param: ids request.IdsReq
//@return: err error

func DeleteBannerByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Banner{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateBanner
//@description: 更新Banner记录
//@param: banner *model.Banner
//@return: err error

func UpdateBanner(banner model.Banner) (err error) {
	err = global.GVA_DB.Save(&banner).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBanner
//@description: 根据id获取Banner记录
//@param: id uint
//@return: err error, banner model.Banner

func GetBanner(id uint) (err error, banner model.Banner) {
	err = global.GVA_DB.Where("id = ?", id).First(&banner).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetBannerInfoList
//@description: 分页获取Banner记录
//@param: info request.BannerSearch
//@return: err error, list interface{}, total int64

func GetBannerInfoList(info request.BannerSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Banner{})
    var banners []model.Banner
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&banners).Error
	return err, banners, total
}